const config = {
	websocketServer: 'ws://47.114.37.145:8000'
}
export default class Socket{
	constructor(arg) {
	    this.socket = null
		this.socketOpen = false
	}
	ws_connect(reMsg, option){
		var url = `${config.websocketServer}/login?{"Method":"login", "Body": {"AppId": "${option.ukeyid}", "AppKey": "www.zhiyuext.com" "Name": "${option.nickname}", "Token": "${option.token}"}}`
		console.log(url)
		this.socket = uni.connectSocket({
			url,
			success: (res)=>{
				console.log('创建连接')
				console.log(res)
			}
		})
		this.socket.onOpen(res => {
			this.socketOpen = true
		})
		this.socket.onClose(onClose => {
			this.socketOpen = false
			console.log('websocket关闭')
		})
		this.socket.onError(onError => {
			this.socketOpen = false
			console.log('websocket错误:', onError)
		})
		this.socket.onMessage(msg => {
			reMsg(msg)
		})
	}
	login(reMsg, ukeyid, nickname, token){
		this.ws_connect(reMsg, {ukeyid, nickname, token})
	}
	sendMsg(msg, success){
		if(this.socketOpen){
			this.socket.send({
				data: JSON.stringify(msg)
			}, function(res){
				success(res)
			})
		}
	}
}